package com.example.demo.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 刘二朋
 * @date 2020-11-30 14:24
 */
public class Tool {


    /**
     * 上下文对象
     */
    public static String Context = "http://192.168.2.122:8080";

    /**
     * 图片的路径
     */
    public static String imagePath = "/images";


    /**
     * 默认头像的地址
     */
    public static String[] images = {"/001.bmp", "/002.png", "/003.bmp",
            "/004.bmp", "/005.bmp", "/006.bmp",
            "/007.bmp", "/008.bmp", "/009.jpg",
    };


    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取上下文对象的方法
     *
     * @return
     */
    public static String getDomain() {
        HttpServletRequest request = getHttpServletRequest();
        String requestURL = request.getRequestURL().toString();//得到请求的URL地址
        String[] split = requestURL.split("/");
        String s = split[0] + "/" + split[1] + "/" + split[2];
        return s;
    }


    public static String getOrigin() {
        HttpServletRequest request = getHttpServletRequest();
        return request.getHeader("Origin");
    }


}
